package com.hgd.security.evp.biz.schedule;

import com.alibaba.fastjson.JSONObject;
import com.hgd.security.evp.biz.biz.InspectionSituationBiz;
import com.hgd.security.evp.biz.entity.InspectionSituation;
import com.hgd.security.evp.biz.websocket.ChatHandler;
import com.hgd.security.evp.common.util.BooleanUtil;
import com.hgd.security.evp.common.util.SpringContextUtil;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Random;

/**
 * @author By 尚
 * @date 2019/4/8 20:04
 */
//@Configuration
//@EnableScheduling
//@Slf4j
public class InspectionSituationSchedule {

    private int[] datas =
        { 26, 25, 24, 24, 23, 22, 22, 22, 21, 21, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18, 18, 17, 17,
            17, 17, 17, 17, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 14, 14, 14, 14, 13, 13, 13,
            13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 9,
            9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 4,
            4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0 };

    @Scheduled(cron = "0/3 * * * * ? ")
    public void addInspectionSituation() {
        try {
            InspectionSituationBiz inspectionSituationBiz =
                SpringContextUtil.getBeanByClassType(InspectionSituationBiz.class);
            ChatHandler chatHandler = SpringContextUtil.getBeanByClassType(ChatHandler.class);
            assist0(inspectionSituationBiz);
            assist1(inspectionSituationBiz,chatHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void assist1(InspectionSituationBiz inspectionSituationBiz, ChatHandler chatHandler) {
        JSONObject jsonObject = inspectionSituationBiz.lastestSituation();
        chatHandler.channelWriteAndFlush(jsonObject.toJSONString());
    }

    private void assist0(InspectionSituationBiz inspectionSituationBiz) throws Exception{
        Random ran = new Random();
        int i = ran.nextInt(datas.length);
        int data = datas[i];

        InspectionSituation situation = new InspectionSituation();
        situation.setHarmfulComId(1);
        situation.setActualConcentration(data * 1.0);
        if (data >= 25) {
            situation.setIsExcessived(BooleanUtil.BOOLEAN_TRUE);
        } else {
            situation.setIsExcessived(BooleanUtil.BOOLEAN_FALSE);
        }


        inspectionSituationBiz.insertSelectiveBindCrt(situation);

        inspectionSituationBiz.deleteById(situation.getId() - 50);
    }
}
